#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include "video.h"
#include "../utils/log.h"

// 全局变量
static bool g_is_initialized = false;
static bool g_is_recording = false;
static video_config_t g_current_config;
static pthread_mutex_t g_video_mutex = PTHREAD_MUTEX_INITIALIZER;

int video_init(void)
{
    pthread_mutex_lock(&g_video_mutex);
    
    if (g_is_initialized) {
        pthread_mutex_unlock(&g_video_mutex);
        return VIDEO_SUCCESS;
    }
    
    // 初始化视频模块资源
    memset(&g_current_config, 0, sizeof(video_config_t));
    g_current_config.codec = VIDEO_CODEC_H264;
    g_current_config.resolution = VIDEO_RES_1080P;
    g_current_config.frame_rate = 30;
    g_current_config.bit_rate = 5000000; // 5Mbps
    strncpy(g_current_config.save_path, "/tmp/video", sizeof(g_current_config.save_path) - 1);
    
    LOG_INFO("视频模块初始化成功");
    g_is_initialized = true;
    
    pthread_mutex_unlock(&g_video_mutex);
    return VIDEO_SUCCESS;
}

void video_cleanup(void)
{
    pthread_mutex_lock(&g_video_mutex);
    
    if (!g_is_initialized) {
        pthread_mutex_unlock(&g_video_mutex);
        return;
    }
    
    // 如果正在录像，先停止
    if (g_is_recording) {
        video_stop_recording();
    }
    
    LOG_INFO("视频模块清理完成");
    g_is_initialized = false;
    
    pthread_mutex_unlock(&g_video_mutex);
}

int video_start_recording(video_config_t *config)
{
    pthread_mutex_lock(&g_video_mutex);
    
    if (!g_is_initialized) {
        pthread_mutex_unlock(&g_video_mutex);
        LOG_ERROR("视频模块未初始化");
        return VIDEO_ERROR_INIT;
    }
    
    if (g_is_recording) {
        pthread_mutex_unlock(&g_video_mutex);
        LOG_WARN("已经在录像中，无需重复开始");
        return VIDEO_SUCCESS;
    }
    
    // 如果传入了配置参数，则更新配置
    if (config != NULL) {
        memcpy(&g_current_config, config, sizeof(video_config_t));
    }
    
    // 模拟开始录像过程
    LOG_INFO("开始录像，编码器:%d, 分辨率:%d, 帧率:%d, 码率:%d, 保存路径:%s", 
             g_current_config.codec, 
             g_current_config.resolution, 
             g_current_config.frame_rate, 
             g_current_config.bit_rate, 
             g_current_config.save_path);
    
    // 这里应该调用实际的视频采集和编码API
    // ...
    
    g_is_recording = true;
    pthread_mutex_unlock(&g_video_mutex);
    return VIDEO_SUCCESS;
}

int video_stop_recording(void)
{
    pthread_mutex_lock(&g_video_mutex);
    
    if (!g_is_initialized) {
        pthread_mutex_unlock(&g_video_mutex);
        LOG_ERROR("视频模块未初始化");
        return VIDEO_ERROR_INIT;
    }
    
    if (!g_is_recording) {
        pthread_mutex_unlock(&g_video_mutex);
        LOG_WARN("没有在录像中，无需停止");
        return VIDEO_SUCCESS;
    }
    
    // 模拟停止录像过程
    LOG_INFO("停止录像");
    
    // 这里应该调用实际的停止API
    // ...
    
    g_is_recording = false;
    pthread_mutex_unlock(&g_video_mutex);
    return VIDEO_SUCCESS;
}

bool video_is_recording(void)
{
    bool ret;
    pthread_mutex_lock(&g_video_mutex);
    ret = g_is_recording;
    pthread_mutex_unlock(&g_video_mutex);
    return ret;
}

int video_set_config(video_config_t *config)
{
    pthread_mutex_lock(&g_video_mutex);
    
    if (!g_is_initialized) {
        pthread_mutex_unlock(&g_video_mutex);
        LOG_ERROR("视频模块未初始化");
        return VIDEO_ERROR_INIT;
    }
    
    if (g_is_recording) {
        pthread_mutex_unlock(&g_video_mutex);
        LOG_ERROR("录像过程中无法修改配置");
        return VIDEO_ERROR_CONFIG;
    }
    
    if (config == NULL) {
        pthread_mutex_unlock(&g_video_mutex);
        LOG_ERROR("配置参数为空");
        return VIDEO_ERROR_CONFIG;
    }
    
    memcpy(&g_current_config, config, sizeof(video_config_t));
    LOG_INFO("视频配置已更新");
    
    pthread_mutex_unlock(&g_video_mutex);
    return VIDEO_SUCCESS;
}